package ht.android.tiengui;

import java.text.NumberFormat;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidTGQuangActivity extends Activity {
	EditText txtSoTien, txtSoThang, txtLaiSuat;
	Button btnThucHien;
	TextView txtShow;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		txtSoTien = (EditText) findViewById(R.id.txtSoTien);
		txtSoThang = (EditText) findViewById(R.id.txtSoThang);
		txtLaiSuat = (EditText) findViewById(R.id.txtLaiSuat);
		btnThucHien = (Button) findViewById(R.id.btnThucHien);
		txtShow = (TextView) findViewById(R.id.txtShow);
		// <-- Lỗi khi sử lý tính toán vd: 2,000+1=2,001(theo em nó chuyễn qua
		// có dấu ,
		// tính toán ko dc

		txtSoTien.addTextChangedListener(new TextWatcher() {
			private String current = "";

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (!s.toString().equals("")) {
					if (!s.toString().equals(current)) {

						String cleanString = s.toString()
								.replaceAll("[,.]", "");

						double parsed = Double.parseDouble(cleanString);

						String formated = NumberFormat.getInstance().format(
								(parsed));

						current = formated;

						txtSoTien.setText(formated);
						
						//txtSoTien.req(R.id.txtSoTien);
						txtSoTien.setSelection(formated.length());
					}
				}

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		btnThucHien.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (txtSoTien.getText().toString().equals("")
						|| txtLaiSuat.getText().toString().equals("")
						|| txtSoThang.getText().toString().equals("")) {
					// Toast.makeText(getApplicationContext(),
					// "Có lỗi. Bạn chưa đủ  \n 3 trường dữ liệi",
					// Toast.LENGTH_LONG).show();

					AlertDialog.Builder builder = new AlertDialog.Builder(
							AndroidTGQuangActivity.this);
					builder.setTitle("Thông Báo");
					builder.setMessage("Bạn chưa điền đủ ba trường dữ liệu. Xin Nhập lại");
					builder.setPositiveButton("Tiếp tục",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// TODO Auto-generated method stub
								}
							});
					builder.show();
				}

				else {

					String strTien = txtSoTien.getText().toString()
							.replaceAll("[,.]", "");
					Float sotien = Float
							.valueOf(strTien);//
					Float sothang = Float.valueOf(txtSoThang.getText()
							.toString());
					Float laisuat = Float.valueOf(txtLaiSuat.getText()
							.toString());
					Float show = ((laisuat / 12) / 100) * sotien * sothang;

					txtShow.setText("Tiền Lãi là: " + String.valueOf(show));

				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.activity_gui_tiet_kiem, menu);
		return true;
	}

}
